<template>
	<view class="content">
		<cu-custom bgColor="bg-gradual-blue" :isBack="true">
			<block slot="content">{{i18n.register}}</block>
		</cu-custom>
		<view class="login-bg">
			<view class="login-card">
				<view class="login-head">{{i18n.regUserTips}}</view>
				<view class="login-input login-margin-b">
					<input type="text" v-model="phone" :placeholder="i18n.phoneHint" />
				</view>
				<view class="login-input login-margin-b">
					<input type="password" v-model="password" :placeholder="i18n.regPsdHint" />
				</view>
				<view class="login-input margin-bottom-xl">
					<input type="password" v-model="repeatPsd" :placeholder="i18n.repeatPsdHint" />
				</view>

			</view>
		</view>
		<view class="login-btn">
			<button class="landing" @click="go_login()" type="primary">{{i18n.register}}</button>
		</view>
	</view>
</template>

<script>
	export default {
		computed: {
			i18n() {
				return this.$t('login')
			},
		},
		data() {
			return {
				phone: '',
				password: '',
				repeatPsd: ''
			}
		},
		onLoad() {

		},
		methods: {
			// go_forget() {
			// 	uni.navigateTo({
			// 		url: 'forget'
			// 	})
			// },
			go_register() {
				uni.navigateTo({
					url: 'register'
				})
			},
			go_login() {
				if (this.phone.length <= 5) {
					uni.showToast({
						title: this.i18n.inputUserHint,
						icon: 'none'
					})
					return
				}else if(!this.checkPhone(this.phone)){
					uni.showToast({
						title: this.i18n.phoneIncorrect,
						icon: 'none'
					})
					return
				}
				if (this.password.length <= 0) {
					uni.showToast({
						title: this.i18n.inputPsdHint,
						icon: 'none'
					})
					return
				} else if (!this.checkPass(this.password)) {
					uni.showToast({
						title: this.i18n.psdSoShort,
						icon: 'none'
					})
					return
				}
				if (this.repeatPsd.length <= 0) {
					uni.showToast({
						title: this.i18n.repeatPsdHint,
						icon: 'none'
					})
					return
				}

				uni.request({
					url: this.$constant.BASEURL + this.$constant.REGISTER_USER,
					method: "POST",
					header: {
						'content-type': 'application/x-www-form-urlencoded'
					},
					data: {
						phone: this.phone,
						pwd: this.password,
						type: 2
					},
					success: (res) => {
						var result = res.data
						console.log(result)
						// console.log(result)
						uni.showToast({
							title:result.message,
							icon:'none'
						})
						if(result.code == '0'){
							//保存用户信息
							this.saveUserInfo()
							uni.navigateBack({
								delta:1
							})
						}
					}
				});
			},
			checkPhone(number) {
				// return /^((13[0-9])|(14[0-9])|(15[0-9])|(17[0-9])|(18[0-9]))\d{8}$/.test(number)
				return true
			},
			checkPass(password) {
				return /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{8,20}$/.test(password)
			},
			saveUserInfo() {
				uni.setStorage({
					key: 'userinfo',
					data: {
						username: this.phone,
						password: this.password
					},
					success: function() {
						console.log('success');
					}
				});
			},
		}
	}
</script>

<style>
	.landing {
		height: 84upx;
		line-height: 84upx;
		border-radius: 44upx;
		font-size: 32upx;
		background: linear-gradient(left, #5599ff, #5599ff);
	}

	.login-btn {
		padding: 10upx 20upx;
		margin-top: 420upx;
	}

	.login-function {
		overflow: auto;
		padding: 20upx 20upx 30upx 20upx;
	}

	.login-forget {
		float: left;
		font-size: 26upx;
		color: #999;
	}

	.login-register {
		color: #666;
		float: right;
		font-size: 26upx;

	}

	.login-input input {
		background: #F2F5F6;
		font-size: 28upx;
		padding: 10upx 25upx;
		height: 62upx;
		line-height: 62upx;
		border-radius: 8upx;
	}

	.login-margin-b {
		margin-bottom: 25upx;
	}

	.login-input {
		padding: 10upx 20upx;
	}

	.login-head {
		font-size: 34upx;
		text-align: center;
		padding: 25upx 10upx 55upx 10upx;
	}

	.login-card {
		background: #fff;
		border-radius: 12upx;
		padding: 10upx 25upx;
		box-shadow: 0 6upx 18upx rgba(0, 0, 0, 0.12);
		position: relative;
		margin-top: 120upx;
	}

	.login-bg {
		height: 260upx;
		padding: 25upx;
		background: linear-gradient(#5599ff, #5599ff);
	}
</style>
